package auction;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import util.TimeSpan;

public class Auction {
	/** 
	 主键 
	*/
	private int id;

	public int getId()
	{
		return id;
	}
	public void setId(int value)
	{
		id = value;
	}

	/** 
	 拍卖玩家的ID	 
	*/
	private int playerId = -1;

	public int getPlayerId()
	{
		return playerId;
	}
	public void setPlayerId(int value)
	{
		playerId = value;
	}

	/** 
	 买家ID	 
	*/
	private int buyerId = -1;

	public int getBuyerId()
	{
		return buyerId;
	}
	public void setBuyerId(int value)
	{
		buyerId = value;
		// 设置购买者id时，需同步到内存表中
		auctionRow.getItem("BuyerID").setYuanColumnText(String.valueOf(value));
	}

	/** 
	 物品ID
	 
	*/
	private String itemId = "";

	public String getItemId()
	{
		return itemId;
	}
	public   void setItemId(String value)
	{
		itemId = value;
	}

	/** 
	 物品等级
	 
	*/
	private int itemLvl = -1;

	public int getItemLvl()
	{
		return itemLvl;
	}
	public void setItemLvl(int value)
	{
		itemLvl = value;
	}

	/** 
	 物品品质
	 
	*/
	private int itemQuality = -1;

	public int getItemQuality()
	{
		return itemQuality;
	}
	public void setItemQuality(int value)
	{
		itemQuality = value;
	}


	/** 
	 装备类型(也就是装备的位置)
	 
	*/
	private int equipType = -1;

	public int getEquipType()
	{
		return equipType;
	}
	public void setEquipType(int value)
	{
		equipType = value;
	}

	/** 
	 物品所属职业
	 
	*/
	private int professionType = -1;

	public int getProfessionType()
	{
		return professionType;
	}
	public void setProfessionType(int value)
	{
		professionType = value;
	}

	/** 
	 材料类型（也就是消耗品类型）
	 
	*/
	private String materialType = "";

	public String getMaterialType()
	{
		return materialType;
	}
	public void setMaterialType(String value)
	{
		materialType = value;
	}

	/** 
	 消耗品id
	 
	*/
	private String consumablesID = "";
	public String getConsumablesID()
	{
		return consumablesID;
	}
	public void setConsumablesID(String value)
	{
		consumablesID = value;
	}

	/** 
	 消耗品数量
	 
	*/
	private int consumablesNum = -1;

	public int getConsumablesNum()
	{
		return consumablesNum;
	}
	public void setConsumablesNum(int value)
	{
		consumablesNum = value;
	}

	/** 
	 建立拍卖信息的时间
	 
	*/
	private java.util.Date startTime = new java.util.Date(0);

	public java.util.Date getStartTime()
	{
		return startTime;
	}
	public void setStartTime(java.util.Date value)
	{
		startTime = value;
	}

	/** 
	 拍卖时长(单位s)
	 
	*/
	private int auctionTime = -1;

	public int getAuctionTime()
	{
		return auctionTime;
	}
	public void setAuctionTime(int value)
	{
		auctionTime = value;
	}

	/** 
	 到期时间
	 
	*/
	private java.util.Date endTime = new java.util.Date(0);

	public java.util.Date getEndTime()
	{
		return endTime;
	}
	public void setEndTime(java.util.Date value)
	{
		endTime = value;
		// 设置拍卖结束时间时，需同步到内存表中
		auctionRow.getItem("EndTime").setYuanColumnText(value.toString());
	}

	/** 
	 拍卖是否有效
	 
	*/
	private boolean isActive = false;

	public boolean getIsActive()
	{
		return isActive;
	}
	public void setIsActive(boolean value)
	{
		isActive = value;
		// 设置拍卖是否有效时，需同步到内存表中
		auctionRow.getItem("IsActive").setYuanColumnText(String.valueOf(value));
	}

	/** 
	 初始价格
	 
	*/
	private int auctionPrice = 0;

	public int getAuctionPrice()
	{
		return auctionPrice;
	}
	public void setAuctionPrice(int value)
	{
		auctionPrice = value;
	}

	private MemoryDB.YuanRow auctionRow;

	public Auction(MemoryDB.YuanRow auctionRow)
	{
		this.auctionRow = auctionRow;
		this.id = Integer.parseInt(auctionRow.getItem("id").getYuanColumnText());
		this.playerId = Integer.parseInt(auctionRow.getItem("PlayerID").getYuanColumnText());
		this.buyerId = this.isNullOrEmpty(auctionRow.getItem("BuyerID").getYuanColumnText()) ? this.buyerId : Integer.parseInt(auctionRow.getItem("BuyerID").getYuanColumnText());
		this.itemId = this.isNullOrEmpty(auctionRow.getItem("ItemID").getYuanColumnText()) ? this.itemId : auctionRow.getItem("ItemID").getYuanColumnText();
		this.itemLvl = this.isNullOrEmpty(auctionRow.getItem("ItemLvl").getYuanColumnText()) ? this.itemLvl : Integer.parseInt(auctionRow.getItem("ItemLvl").getYuanColumnText());
		this.itemQuality = this.isNullOrEmpty(auctionRow.getItem("ItemQuality").getYuanColumnText()) ? this.itemQuality : Integer.parseInt(auctionRow.getItem("ItemQuality").getYuanColumnText());
		this.equipType = this.isNullOrEmpty(auctionRow.getItem("EquipType").getYuanColumnText()) ? this.equipType : Integer.parseInt(auctionRow.getItem("EquipType").getYuanColumnText());
		this.professionType = this.isNullOrEmpty(auctionRow.getItem("ProfessionType").getYuanColumnText())? this.professionType : Integer.parseInt(auctionRow.getItem("ProfessionType").getYuanColumnText());
		this.materialType = this.isNullOrEmpty(auctionRow.getItem("MaterialType").getYuanColumnText()) ? this.materialType : auctionRow.getItem("MaterialType").getYuanColumnText();
		this.consumablesID = this.isNullOrEmpty(auctionRow.getItem("ConsumablesID").getYuanColumnText()) ? this.consumablesID : auctionRow.getItem("ConsumablesID").getYuanColumnText();
		this.consumablesNum = this.isNullOrEmpty(auctionRow.getItem("ConsumablesNum").getYuanColumnText()) ? this.consumablesNum : Integer.parseInt(auctionRow.getItem("ConsumablesNum").getYuanColumnText());
		this.startTime = this.isNullOrEmpty(auctionRow.getItem("StartTime").getYuanColumnText()) ? this.startTime : stringToDate(auctionRow.getItem("StartTime").getYuanColumnText(), this.startTime);
		this.auctionTime = this.isNullOrEmpty(auctionRow.getItem("AuctionTime").getYuanColumnText()) ? this.auctionTime : Integer.parseInt(auctionRow.getItem("AuctionTime").getYuanColumnText());
		this.endTime = this.isNullOrEmpty(auctionRow.getItem("EndTime").getYuanColumnText()) ? this.endTime : stringToDate(auctionRow.getItem("EndTime").getYuanColumnText(), this.endTime);
		this.isActive = this.isNullOrEmpty(auctionRow.getItem("IsActive").getYuanColumnText()) ? this.isActive : Boolean.parseBoolean(auctionRow.getItem("IsActive").getYuanColumnText());
		this.auctionPrice = this.isNullOrEmpty(auctionRow.getItem("AuctionPrice").getYuanColumnText()) ? this.auctionPrice : Integer.parseInt(auctionRow.getItem("AuctionPrice").getYuanColumnText());
	}

	/** 
	 更新本拍卖物品的信息
	 
	*/
	public void Update()
	{
		if (this.isActive && new TimeSpan(new java.util.Date(), this.getStartTime()).getSeconds() > this.getAuctionTime())
		{
			EndAuction();
		}
	}

	/** 
	 结束拍卖
	 
	*/
	public void EndAuction()
	{
		this.setEndTime(new java.util.Date());
		this.setIsActive(false);
	}
	
	/**
	 * 判断字符串是否为null或空
	 * @param str
	 * @return
	 */
	private boolean isNullOrEmpty(String str)
	{
		if(null == str || str.equals(""))
		{
			return true;
		}
		
		return false;
	}
	
	private java.util.Date stringToDate(String str, java.util.Date defaultDate)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.util.Date date = defaultDate;
		
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return date;
	}
}
